// pages/car/car.js
var util = require('../../utils/util.js')
var time= new Date();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    city:'',  
    weekday: '',
    xianxing: '',
    week: ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'],
    xianxinglist:["今日不限行","5和0","1和6","2和7","3和8","4和9","今日不限行"]
  },  

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var dayTime = util.formatTime(new Date());
    var now=dayTime.split(" ")[0].split("/");
    console.log(now);
    var that = this;

    wx.getSetting({//获取用户已授权的信息
      success(res) {        
        if (res.authSetting['scope.userLocation']==false){//如果没有授权地理位置         
          wx.openSetting({
            success(res) {
              res.authSetting = {//打开授权位置页面，让用户自己开启
                "scope.userLocation": true
              }             
            }
          })
        }else{//用户开启授权后可直接获取地理位置
          wx.authorize({
            scope: 'scope.userLocation',
            success() {
             //获取位置操作
             wx.getLocation({
              success: function(res) {
                console.log(res)
                var longitude = res.longitude
                var latitude = res.latitude;
                // this.curCity(longitude, latitude)
                wx.request({
                  url: 'http://api.map.baidu.com/reverse_geocoding/v3/?ak=nMSaDWveTzkeHqSH8vB4Exyv5QAKmSDu&output=json&coordtype=wgs84ll&location=' + latitude + ',' + longitude + '&output=json&pois=1',
                  data:{},
                  header: {
                    'Content-Type': 'application/json'
                  },
                  success:function(res){
                    console.log(res);
                    var city = res.data.result.addressComponent.city
                    console.log("定位的城市"+city);
                    that.setData({
                      city:city
                    })            
                  }
                })
              },
              fail:(err)=>console.log(err)
            })
            }
          })
        }       
      }
    })
    },
   


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var today=new Date().getDay(); 
    console.log("today:"+today);
   switch (today){
       case 0:
       this.setData({
         weekday: this.data.week[0],
         xianxing: this.data.xianxinglist[0]
       }) 
       break; 
       case 1:
       case 2:
       case 3:
       case 4:
       case 5:
       case 6:
      this.setData({
        weekday: this.data.week[today],
        xianxing: this.data.xianxinglist[today]
      })
      console.log(this.data.weekday,this.data.xianxing)
       break;
     }
 },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})